package com.myplans.client.view;

import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.InlineHyperlink;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.myplans.client.AppController.Display;
import com.myplans.client.HistoryTokens;
import com.myplans.client.util.CookieManager;
import com.myplans.client.view.custom.AddPlanDialogBox;
import com.myplans.client.view.custom.SignInDialogBox;
import com.myplans.client.view.custom.SignUpDialogBox;

public class BaseView extends Composite implements HasText, HasWidgets, Display {

	private static BaseViewUiBinder uiBinder = GWT
			.create(BaseViewUiBinder.class);

	interface BaseViewUiBinder extends UiBinder<Widget, BaseView> {
	}

	public BaseView() {
		initWidget(uiBinder.createAndBindUi(this));
		logout.setVisible(false);
		profile.setVisible(false);
	}

	@UiField
	InlineHyperlink signIn;
	@UiField
	InlineHyperlink signUp;
	@UiField
	InlineHyperlink addPlan;
	@UiField
	FlowPanel centralPanel;
	@UiField
	InlineHyperlink logout;
	@UiField
	InlineHyperlink profile;
	@UiField
	Element linksPanel;
	@UiField
	TextBox searchTextBox;
	
	SignInDialogBox signInDialogBox = new SignInDialogBox();
	SignUpDialogBox signUpDialogBox = new SignUpDialogBox();
	AddPlanDialogBox addPlanDialogBox = new AddPlanDialogBox();
	
	public BaseView(String firstName) {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiHandler("signIn")
	void onSignInClick(ClickEvent e) {
		signInDialogBox.getEmail().setText("");
		signInDialogBox.getPassword().setText("");
		signInDialogBox.show();
	}
	@UiHandler("signUp")
	void onSignUpClick(ClickEvent e) {
		signUpDialogBox.getName().setText("");
		signUpDialogBox.getEmail().setText("");
		signUpDialogBox.getPassword().setText("");
		signUpDialogBox.show();
	}
	@UiHandler("addPlan")
	void onAddPlanClick(ClickEvent e) {
		addPlanDialogBox.getName().setText("");
		addPlanDialogBox.getLocation().setText("");
		addPlanDialogBox.getDescription().setText("");
		addPlanDialogBox.show();
	}
	@UiHandler("logout")
	void onLogoutClicked(ClickEvent e) {
		CookieManager.deleteUserLoginCookies();
		History.newItem(HistoryTokens.LOGOUT.getId());
	}
	@UiHandler("profile")
	void onProfileClicked(ClickEvent e) {
		History.newItem(HistoryTokens.PROFILE.getId());
	}
	
	@Override
	public String getText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setText(String arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public TextBox getSignInEmail() {
		return signInDialogBox.getEmail();
	}

	public PasswordTextBox getSignInPasword() {
		return signInDialogBox.getPassword();
	}

	public Button getSignInButton() {
		return signInDialogBox.getSignInButton();
	}
	
	public TextBox getSignUpEmail() {
		return signUpDialogBox.getEmail();
	}
	
	public TextBox getSignUpName() {
		return signUpDialogBox.getName();
	}

	public PasswordTextBox getSignUpPasword() {
		return signUpDialogBox.getPassword();
	}

	public Button getSignUpButton() {
		return signUpDialogBox.getSignUpButton();
	}

	public FlowPanel getCentralPanel() {
		return centralPanel;
	}

	public AddPlanDialogBox getAddPlanDialogBox() {
		return addPlanDialogBox;
	}

	public InlineHyperlink getSignIn() {
		return signIn;
	}

	public InlineHyperlink getSignUp() {
		return signUp;
	}
	
	public void replaceLinks(boolean replace) {
		signIn.setVisible(!replace);
		signUp.setVisible(!replace);
		logout.setVisible(replace);
		profile.setVisible(replace);
		addPlan.setVisible(replace);
	}

	public InlineHyperlink getLogout() {
		return logout;
	}

	@Override
	public void add(Widget w) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Iterator<Widget> iterator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean remove(Widget w) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void hideDialogBoxes() {
		signInDialogBox.getDialogBox().hide();
		signUpDialogBox.getDialogBox().hide();
		addPlanDialogBox.getDialogBox().hide();
	}
	
	public TextBox getSearchTextBox() {
		return searchTextBox;
	}


}
